package Admin;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dushibao.admin.AdminApplication;
import com.dushibao.admin.model.Admin;
import com.dushibao.admin.service.IAdminService;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.List;

/**
 *  用户信息 测试类
 *
 * @author zqd
 * @date 2021-12-06 08:28:09
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdminApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AdminServiceDemo {

    @Autowired
    IAdminService adminService;

    @Test
    public void getById(){
        Admin admin = this.adminService.getById(1);
        System.out.println(JSON.toJSONString(admin));
        Assert.assertNotNull(admin);
    }

    @Test
    public void list(){
        List<Admin> admins = this.adminService.list();
        admins.forEach(System.out::println);
    }

    @Test
    public void save(){
        Admin admin = new Admin();
        admin.setUserName("小王同学");
        admin.setSex(1);
        admin.setTrueName("王同学");
        admin.setAddTime(new Date());
        admin.setDeleteStatus(0);
        admin.setLoginDate(new Date());
        admin.setLoginIp("127.0.0.1");
        admin.setTelephone("123456789");
        admin.setPassword("123456");

        for(int i=0;i<100;i++){
            this.adminService.save(admin);
        }
    }

    @Test
    public void removeById(){
        boolean ret = this.adminService.removeById(1L);
        Assert.assertEquals(true,ret);


    }

    @Test
    public void saveOrUpdate(){
//        Admin admin = this.adminService.getById(1);
//        boolean ret = this.adminService.saveOrUpdate(admin);
//        Assert.assertEquals(true,ret);

        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("password","1234567");

        this.adminService.update(updateWrapper);

    }

    @Test
    public void page(){
        Integer currentPage = 1;
        Integer pageSize = 20;

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        IPage<Admin> iPage = new Page<>(currentPage,pageSize);
        IPage<Admin> page = this.adminService.page(iPage,queryWrapper);

        page.getRecords().forEach(System.out::println);

    }

}
